package projectEuler.net;
/**
 * What is the largest prime factor of the number 600851475143?
 * @author xrose
 *
 */
public class Problem_003 {
	private static int[] factorRegister;
	private static int factorIndex;
	
	public static int[] factorPrimes(long n){
		init();
		takeFactorToRegister(n);
		return copyToResult();
	}
	private static void init(){
		factorRegister = new int[100];
		factorIndex = 0;
	}
	private static void takeFactorToRegister(long n){
		if(n%2==0)
			for(;n%2==0; n=n/2)
				factorRegister[factorIndex++]=2;
		for(int factor = 3; n!=1; factor+=2)
			for(;n%factor==0; n=n/factor)
				factorRegister[factorIndex++]=factor;
	}
	private static int[] copyToResult(){
		int[] factor = new int[factorIndex];
		for(int i =0; i<factorIndex; i++)
			factor[i]=factorRegister[i];
		return factor;
	}
	public static int maxFactorier(long n){
		int[] result = factorPrimes(n);
		return result[result.length-1];
	}
}
